/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.huangp.jms;

import java.io.File;
import java.util.List;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import net.huangp.dao.TransactionDAO;
import net.huangp.domain.SMS;
import net.huangp.domain.Status;
import net.huangp.domain.Transaction;
import net.huangp.service.SMSService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 *
 * @author Patrick Huang
 */
@Component("smsJmsListener")
public class SMSUploadProcessorJmsListener implements MessageListener{
    //--------------------------------------------------------------------------
    // Static Properties
    //--------------------------------------------------------------------------
    private static final Logger logger = LoggerFactory.getLogger(SMSUploadProcessorJmsListener.class);
    //--------------------------------------------------------------------------
    // Properties
    //--------------------------------------------------------------------------
    private TransactionDAO transactionDAO;
    private SMSService smsService;
    // -------------------------------------------------------------------------
    // Implementation Methods
    // -------------------------------------------------------------------------
    @Autowired
    public SMSUploadProcessorJmsListener(TransactionDAO tranDAO,SMSService smsService){
        this.transactionDAO = tranDAO;
        this.smsService = smsService;
    }

    /**
     * 
     * @param smsUploadMsg a jms message that consists presumably a file path
     *                      and transaction id
     * 
     */
    public void onMessage(Message smsUploadMsg) {
        TextMessage jmsMessage = (TextMessage) smsUploadMsg;
        try {
            logger.info("received jms message with tran id:{}",
                    jmsMessage.getLongProperty("transactionId"));

            //start processing
            File file = new File(jmsMessage.getText());
            Long tranId = jmsMessage.getLongProperty(Transaction.TRANSACTION_ID);
            final Transaction tran = this.transactionDAO.getTransactionById(tranId);

            //check csv file
            if(!file.exists() || !file.canRead()){
                logger.error("can not proceed further.file not exist");
                //set transaction to error
                this.transactionDAO.updateTransactionStatus(tranId, Status.ERROR);
                return;
            }

            //do the real job
            List<SMS> newSMS = this.smsService.parseCSV(file);
            logger.info("{} sms uploaded",newSMS.size());
            this.smsService.bulkSaveSMS(newSMS);

            //if comes to here,update transaction status to complete
            this.transactionDAO.updateTransactionStatus(tranId, Status.COMPLETE);
        }
        catch (JMSException e) {
            logger.error("error consuming jms message",e);
        }
    }
    // -------------------------------------------------------------------------
    // Mutator Methods
    // -------------------------------------------------------------------------
}
